package com.zjitc.service.impl;

import com.mysql.jdbc.interceptors.SessionAssociationInterceptor;
import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import javax.servlet.http.HttpSession;

import com.zjitc.util.CacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by Aspire on 2018/3/28.
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDao userDao;

  @Override
  public ServerResponse login(String username, String password) {
    if ("".equals(username) || username == null || "".equals(password) || password == null) {
      return ServerResponse.createError("参数错误");
    }

    User user = userDao.login(username, password);
    if (user != null) {
      return ServerResponse.createSuccess("登录成功", user);
    }

    return ServerResponse.createError("密码错误");
  }

  @Override
  public ServerResponse register(User user) {
    //用户为空
    if (user == null) {
      return ServerResponse.createError("参数错误");
    }
    //校验用户名是否存在
    int rowCount = userDao.checkUsername(user.getUsername());
    if (rowCount > 0) {
      return ServerResponse.createError("用户已存在");
    }
    //校验注册
    int resultCount = userDao.register(user);
    if (resultCount == 0) {
      return ServerResponse.createError("注册失败");
    }
    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse check_valid(String str, String type) {
    if (type.equals("username")) {
      int count = userDao.checkUsername(str);
      if (count == 1) {
        return ServerResponse.createError("用户已存在");
      }
      return ServerResponse.createSuccess("校验成功");
    }
    if (type.equals("email")) {
      int rcount = userDao.checkEmail(str);
      if (rcount == 1) {
        return ServerResponse.createError("邮箱已存在");
      }
      return ServerResponse.createSuccess("校验成功");
    }

    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse get_user_info(HttpSession session) {
    User user = (User) session.getAttribute("user");
    if (user == null) {
      return ServerResponse.createError("用户未登录,无法获取当前用户信息");
    } else {
      int count = userDao.checkUsername(user.getUsername());
      if (count != 1) {
        return ServerResponse.createError("不存在该用户");
      }
    }
    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse forget_get_question(String username) {
    int count=userDao.checkUsername(username);
    if (count==0){
      return ServerResponse.createError("用户不存在");
    }
    String question=userDao.selectQuestion(username);
    if (question==null) {
      return ServerResponse.createError("该用户未设置找回密码问题");
    }
    return ServerResponse.createSuccess(question);
  }

  @Override
  public ServerResponse forget_check_answer(String username, String question, String answer) {
    int count=userDao.checkAnswer(username, question, answer);
    if (count>0){
      String forgetToken= UUID.randomUUID().toString();
      //设置缓存
      CacheUtil.setValue(username, forgetToken);
      return ServerResponse.createSuccess(forgetToken);
    }
    return ServerResponse.createError("问题答案错误");
  }

  @Override
  public ServerResponse forget_reset_password(String username, String passwordNew, String forgetToken) {
    String token = CacheUtil.getValue(username);
    if (!token.equals(forgetToken)) {
      return ServerResponse.createError("Token已过期");
    }
    int rowCount = userDao.updatePasswordByUsername(username, passwordNew);
    if (rowCount < 0) {
      return ServerResponse.createError("修改密码操作失效");
    }
    return ServerResponse.createSuccess("修改密码成功");
  }


  @Override
  public ServerResponse reset_password(String passwordOld, String passwordNew, User user) {
    int resultCount = userDao.checkPassword(passwordOld,user.getId());
    if(resultCount == 0){
      return ServerResponse.createError("旧密码输入错误");
    }

    user.setPassword(passwordNew);
    int updateCount = userDao.updatePasswordByUsername(user.getUsername(), passwordNew);
    if(updateCount > 0){
      return ServerResponse.createSuccess("修改密码成功");
    }
    return ServerResponse.createError("修改密码失败");
  }

  @Override
  public ServerResponse update_information(User user) {
    int rowCout = userDao.updateUserById(user);
    if (rowCout <= 0) {
      return ServerResponse.createError("用户未登录");
    }
    return ServerResponse.createSuccess("更新个人信息成功");
  }


  @Override
  public ServerResponse<User> get_information(Integer id) {
    User user=userDao.selectById(id);
    if (user==null){
      return ServerResponse.createError("用户未登录,无法获取当前用户信息,status=10,强制登录");
    }
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse logout(HttpSession session) {
    return ServerResponse.createSuccess("退出成功");
  }

}